package com.woniuxy.oa.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oa.entity.Salary;
import com.woniuxy.oa.entity.query.SalaryQuery;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Salary controller.
 *
 * @Auther: 毓
 * @Date: 2019 /12/25 11:45
 * @Description:
 */
@RestController
@CrossOrigin("http://localhost:8081")
@RequestMapping("salary")
public class SalaryController {
    /**
     * The Salary service.
     */
    @Autowired
    private SalaryService salaryService;


    /**
     * Update string.
     *更新员工薪资
     * @param salary the salary
     * @return the string
     */
    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestBody Salary salary) {
        salaryService.update(salary);
        return "redirect:salary/query";
    }

    /**
     * Query response vo.
     *查询员工薪资
     * @param salary the salary
     * @return the response vo
     */
    @PostMapping("/query")
    @ResponseBody
    public ResponseVO query(@RequestBody SalaryQuery salary) {
        PageHelper.startPage(salary.getPageNum() == null ? 1 : salary.getPageNum(), 3);
        List<Salary> salaries = salaryService.getByQuery(salary);
        ResponseVO<PageInfo> vo = new ResponseVO<>();
        PageInfo<Salary> pageInfo = new PageInfo<>(salaries);
        vo.setData(pageInfo);
        return vo;
    }


    /**
     * Update response vo.
     *查看员工的薪资
     * @param id the id
     * @return the response vo
     */
    @PostMapping("/view")
    @ResponseBody
    public ResponseVO<Salary> update(@RequestBody String id) {
        Salary salary = salaryService.getById(id);
        ResponseVO<Salary> vo = new ResponseVO<>();
        vo.setData(salary);
        return vo;
    }
}
